import threading
import time
import inspect
import sys
import pstats
from typing import Callable, Type, Dict, Any, List, Optional
# from simplehtf.utils import threads

class PhaseResult:
    CONTINUE = "CONTINUE"
    REPEAT = "REPEAT"
    SKIP = "SKIP"
    FAIL = "FAIL"
    STOP = "STOP"
    TIMEOUT = "TIMEOUT"
    ERROR = "ERROR"

class PhaseRecord:
    def __init__(self):
        self.phase_name = None
        self.start_time = None
        self.end_time = None
        self.outcome = None

    def to_dict(self):
        return {
            'phase_name':self.phase_name,
            'start_time':self.start_time,
            'end_time':self.end_time,
            'outcome':self.outcome
        }

    def mark_phase_start(self,):
        self.start_time = time.time()

    def mark_phase_finish(self, outcome):
        self.end_time = time.time()
        self.outcome = outcome 


    def set_phase_name(self,phase_name):
        self.phase_name = phase_name
        return self
    
    def set_start_time(self,start_time):
        self.start_time = start_time
        return self
    
    def set_end_time(self,end_time):
        self.end_time = end_time
        return self
    
    def set_outcome(self,outcome):
        self.outcome = outcome
        return self


class PhaseDescriptor:
    def __init__(self, func: Callable ,name = None):
        self.func = func
        self.name = func.__name__

        if name != None:
            self.name = name

    def get_phase_name(self,):
        return self.name

    def __call__(self, output):
        return self.func(output)
